/**
 * 渠道关注统计
 * @return {}
 */
Ext.chart.Chart.CHART_URL = __ctxPath + '/ext/resources/charts.swf';
var followSourceChartForm = function(){
	var followSourceChartPanel = new Ext.form.FormPanel({
				id : 'followSourceChartForm',
				frame : true,
				bodyBorder : false,
				anchor : '100% 10%',
				labelAlign : 'right',
				items : [{
					layout : 'column',
					border : false,
					items : [{
						columnWidth : .25,
						layout : 'form',
						border : false,
						items : [{
							xtype : "datefield",
							id : "followSourceChartStartDate",
							name : "followSourceStartDate",
							fieldLabel : "开始日期",
							format : "Y-m-d",// 日期的格式
							anchor : "100%",
							listeners : {
								'select' : function() {
									Ext.getCmp('followSourceChart_checkBoxDay')
											.reset();
									Ext.getCmp('followSourceChartEndDate')
											.setMinValue(this.getValue());
								}
							}
						}]
					}, {
						columnWidth : .25,
						layout : 'form',
						border : false,
						items : [{
							id : "followSourceChartEndDate",
							xtype : "datefield",
							name : "followSourceEndDate",
							fieldLabel : "结束日期",
							anchor : "100%",
							format : "Y-m-d",// 日期的格式
							listeners : {
								'select' : function() {
									Ext.getCmp('followSourceChart_checkBoxDay')
											.reset();
									Ext.getCmp('followSourceChartStartDate')
											.setMaxValue(this.getValue());
								}
							}
						}]
					}, {
						columnWidth : .25,
						layout : 'form',
						border : false,
						items : [{
							id : 'followSourceChart_checkBoxDay',
							xtype : 'radiogroup',
							labelStyle : "text-align:right;",
							height : 20,
							name : 'radioBoxDay',
							fieldLabel : '天数',
							hideLabels : false,
							items : [{
										xtype : 'radio',
										boxLabel : '30天',
										name : 'radioDay',
										inputValue : 30
									}, {
										xtype : 'radio',
										boxLabel : '90天',
										name : 'radioDay',
										inputValue : 90
									}, {
										xtype : 'radio',
										boxLabel : '180天',
										name : 'radioDay',
										inputValue : 180
									}],
							listeners : {
								change : function(obj, newValue, oldValue,
										eOpts) {// 这事件是当radiogroup的值发生改变时进入
									console.info(newValue);
									if (newValue != null && newValue != ''
											&& newValue != 'null') {
										var followSourceChartStartDate = Ext
												.getCmp('followSourceChartStartDate')
												.setValue('');
										var followSourceChartEndDate = Ext
												.getCmp('followSourceChartEndDate')
												.setValue('');
									}

								}
							}
						}]
					}, {
						columnWidth : .25,
						layout : 'form',
						border : false,
						items : [{
							layout : 'column',
							border : false,
							items : [{
										text : '查询',
										xtype : 'button',
										width : 80,
										handler : function() {
											followSourceChartHandler();
										},
										style : {
											marginLeft : "100px",
											marginRight : "10px"
										}
									}, {
										text : '重置',
										xtype : 'button',
										width : 80,
										handler : function() {
											Ext.getCmp('followSourceChartForm')
													.getForm().reset();
											// 对日期的初始设置
											Ext
													.getCmp('followSourceChartStartDate')
													.setMaxValue(new Date());
											Ext.getCmp('followSourceChartEndDate')
													.setMinValue("");
										}
									}]
						}]
					}]
				}]
			});
	return followSourceChartPanel;
}
/**
 * 图表
 * 
 * @return {}
 */
var getPanelView = function() {
	// 定义toolbar
	var statisticFollowSourceTb = new Ext.Toolbar({
				id : "statisticFollowSourceTb",
				height : 25,
				items : []
			});
	statisticFollowSourceTb.add({
				xtype : 'displayfield',
				value : '渠道关注趋势图',
				style : 'margin-left : 10px;'
			},{
				xtype : 'displayfield',
				value : '安家DM：',
				style : 'margin-left : 30px;'
			}, {
				xtype : 'displayfield',
				id : 'followSourceAnjia'
			},{
				xtype : 'displayfield',
				value : '暖心活动：',
				style : 'margin-left : 30px;'
			}, {
				xtype : 'displayfield',
				id : 'followSourceWarmActivity'
			},{
				xtype : 'displayfield',
				value : '渠道麟泉：',
				style : 'margin-left : 30px;'
			}, {
				xtype : 'displayfield',
				id : 'followSourceLinQuan'
			});

	var rosePieChart = new Ext.Panel({
				anchor : '100% 90%',
				html : "<div id='followSourceChartdiv'></div>",
				tbar : statisticFollowSourceTb,
				listeners : {
					"afterrender" : followSourceChartHandler
				}
			});
	return rosePieChart;
}

/**
 * 订单增长趋势图
 */
function followSourceChartHandler() {
	var followSourceChartForm = Ext.getCmp('followSourceChartForm');
	// 请求数据
	Ext.Ajax.request({
				url : __ctxPath + "/statisticManager/selectFollowSourceChart",
				params : followSourceChartForm.getForm().getValues(),
				method : "post",
				success : function(response, config) {
					var jsonData = Ext.util.JSON.decode(response.responseText);
						var qrsceneFollowList = jsonData.list.qrsceneFollowList;
						var totalFollowTimesAnjia = 0;
						var totalCancelFollowTimesAnjia = 0;
						var totalFollowTimesWarmActivity = 0;
						var totalCancelFollowTimesWarmActivity = 0;
						var totalFollowTimesLinQuan = 0;
						var totalCancelFollowTimesLinQuan= 0;
						if(qrsceneFollowList!=null &&qrsceneFollowList !='' ){
							for(var i = 0; i < qrsceneFollowList.length; i++ ){
							var tmp = qrsceneFollowList[i];
							var followTimes = tmp.followTimes;
							var cancelFollowTimes = tmp.cancelFollowTimes;
							var anjia = tmp.label;
							var warmActivity = tmp.label;
							var linQuan = tmp.label;
								if(anjia == "qrscene_1"){
									totalFollowTimesAnjia += followTimes;
									totalCancelFollowTimesAnjia += cancelFollowTimes;
								}else if(warmActivity == "qrscene_2"){
									totalFollowTimesWarmActivity += followTimes;
									totalCancelFollowTimesWarmActivity += cancelFollowTimes;
								}else if(linQuan == "qrscene_3"){
									totalFollowTimesLinQuan += followTimes;
									totalCancelFollowTimesLinQuan += cancelFollowTimes;
								}
							}
						}
						Ext.getCmp('followSourceAnjia')
							.setValue('<div style="color:red;">' +'(关注次数：'+totalFollowTimesAnjia  +',取消关注：'+totalCancelFollowTimesAnjia+')'+'</div>');
						Ext.getCmp('followSourceWarmActivity')
							.setValue('<div style="color:red;">' +'(关注次数：'+totalFollowTimesWarmActivity  +',取消关注：'+totalCancelFollowTimesWarmActivity+')'+'</div>');
						Ext.getCmp('followSourceLinQuan')
							.setValue('<div style="color:red;">' +'(关注次数：'+totalFollowTimesLinQuan  +',取消关注：'+totalCancelFollowTimesLinQuan+')'+'</div>');
					if (jsonData.success) {
						var myChart = new FusionCharts({
									type : 'scrollline2d',
									renderAt : 'followSourceChartdiv',
									width : "100%",
									height : "100%",
									dataFormat : 'json',
									dataSource : {
										"chart" : {
											"xaxisname" : "日期",
											"yaxisname" : "关注数",
											"palette" : "3",
											"bgcolor" : "FFFFFF",
											"canvasbgcolor" : "66D6FF",
											"canvasbgalpha" : "5",
											"canvasborderthickness" : "1",
											"canvasborderalpha" : "20",
											"legendshadow" : "0",
											"showvalues" : "0",
											"alternatehgridcolor" : "ffffff",
											"alternatehgridalpha" : "100",
											"showborder" : "0",
											"legendborderalpha" : "0",
											"legendiconscale" : "1.5",
											"divlineisdashed" : "1",
											showZeroPlane  : 0,
											showLimits:1
										},
										"categories" : [{
													"category" :jsonData.list.dayList
												}],
										"dataset" : [{
													"seriesname" : "安家DM",
													"color" : "00FF00",
													"data" :jsonData.list.qrsceneList
												},{
													"seriesname" : "暖心活动",
													"color" : "0000FF",
													"data" :jsonData.list.warmActivityList
												},{
													"seriesname" : "渠道麟泉",
													"color" : "#660077",
													"data" :jsonData.list.linQuanList
												}],
										"styles" : {
											"definition" : [{
														"name" : "captionFont",
														"type" : "font",
														"size" : "15"
													}],
											"application" : [{
														"toobject" : "caption",
														"styles" : "captionfont"
													}]
										}
									}
								});
						myChart.render();
					} else {
						Ext.MessageBox.show({
									title : "操作信息",
									msg : "订单类型统计数据获取失败!",
									buttons : Ext.MessageBox.OK,
									icon : Ext.MessageBox.ERROR
								});
					}
				},
				failure : function(response, config) {
					Ext.MessageBox.show({
								title : "错误提示",
								msg : "服务器请求异常，请稍后再试!",
								buttons : Ext.MessageBox.OK,
								icon : Ext.MessageBox.ERROR
							});
				}
			});

}

Ext.getCmp("centerTabPanel").getActiveTab().add(followSourceChartForm(),
		getPanelView());
Ext.getCmp("centerTabPanel").getActiveTab().doLayout();